﻿using System;
using System.ComponentModel;
using MECPSDB.EntityClasses;
using MECPSDB.FactoryClasses;
using MECPSDB.HelperClasses;
using MECUITMPLTLIB;
using MECPSDB.DatabaseSpecific;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace MEC.WMS.FileMaintenance.UoM {
    
    public partial class AeFrm:AeFrmUiTemplate{
        
        private const string Caption = "UoM Maintenance";
        
        public AeFrm(string cs, string branchcode, string usercode){
            if (cs == null){
                throw new ArgumentNullException("cs");
            }
            Cs= cs;
            Branchcode = branchcode;
            Usercode = usercode;
            InitializeComponent();
            this.Text = Caption;
        }

        public override sealed string Text{
            get{
                return base.Text;
            }
            set{
                base.Text = value;
            }
        }

        public override void MethodNewEntityProcedure(){
           base.MethodNewEntityProcedure();
           MatumEntity entity = new MatumEntity(); //-implementation
           //todo Add extra code here
           //... 
           MainEntity = entity; //-implementation
           
           MethodAllowEdit(true); ////-implementation
       }

        protected override void MainEntityBackgroundSaveDoWork(object sender, DoWorkEventArgs e){
            MatumEntity entity = (MatumEntity) e.Argument;
            using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(Cs)){

                if (entity.IsNew){} else{}

                dataAccessAdapter.SaveEntity(entity, true);
                e.Result = entity;
            }

        }

        protected override void MethodLoadControlValuesToObjectsForSaving(){
            base.MethodLoadControlValuesToObjectsForSaving();
            MatumEntity entity = (MatumEntity) MainEntity;
            entity.UomCode = (string) textEditUomCode_ae_pk_.EditValue;
            entity.UomDescription = (string) textEditDescription_ae_.EditValue;
            entity.Remarks = (string) memoEditRemakrs_ae_.EditValue;
        }

        protected override bool MethodValidate(){
            dxErrorProvider1.ClearErrors();
            bool focused = false;
            if (string.IsNullOrWhiteSpace(textEditUomCode_ae_pk_.Text)){
                dxErrorProvider1.SetError(textEditUomCode_ae_pk_, "Required");
                textEditUomCode_ae_pk_.Focus();
                focused = true;
            }
            if (string.IsNullOrWhiteSpace(textEditDescription_ae_.Text)){
                dxErrorProvider1.SetError(textEditDescription_ae_, "Required");
                if (!focused){
                    textEditDescription_ae_.Focus();
                }
            }

            return !dxErrorProvider1.HasErrors;
        }

        protected override void MethodDispayMainEntityToControls(){
            base.MethodDispayMainEntityToControls();

            MatumEntity entity = (MatumEntity) MainEntity;
            textEditUomCode_ae_pk_.EditValue = entity.UomCode;
            textEditDescription_ae_.EditValue = entity.UomDescription;
            memoEditRemakrs_ae_.EditValue = entity.Remarks;
        }

        protected override void MainEntityBackgroundFetchDoWork(object sender, DoWorkEventArgs e){
            FetchParam fetchParam = (FetchParam) e.Argument;
            //BackgroundWorker bw = (BackgroundWorker) sender;
            //TODONotNeeded LookupFetches
            //if (fetchParam.FetchLookupTables){
            //    EntityCollection ectbrand = new EntityCollection(new MatbrandEntityFactory());
            //    EntityCollection ectgroup = new EntityCollection(new MatgroupEntityFactory());
            //    EntityCollection ectuom = new EntityCollection(new MatumEntityFactory());
            //    using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(fetchParam.Cs)){
            //        dataAccessAdapter.FetchEntityCollection(ectbrand, null);
            //        bw.ReportProgress(1, ectbrand);
            //        dataAccessAdapter.FetchEntityCollection(ectgroup, null);
            //        bw.ReportProgress(2, ectgroup);
            //        dataAccessAdapter.FetchEntityCollection(ectuom, null);
            //        bw.ReportProgress(3, ectuom);
            //    }
            //}

            //TODO MainEntityFetch
            if (fetchParam.FetchMainEntity){
                MatumEntity entity = new MatumEntity(fetchParam.PkCode);
                IPrefetchPath2 prefetchPath = null;
                ExcludeIncludeFieldsList excludedfields = new ExcludeIncludeFieldsList();
                using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(fetchParam.Cs)){
                    dataAccessAdapter.FetchEntity(entity, prefetchPath, null, excludedfields);
                    fetchParam.MainEntityFetched = entity;
                    e.Result = fetchParam;
                    return;
                }
            }
        }


        
    }//End class
} // End namespace